package com.sixth.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sixth.dao.RoleDao;
import com.sixth.entity.Role;
import com.sixth.entity.User;
import com.sixth.service.RoleService;
import com.sixth.util.CommonResult;
import com.sixth.util.TableLayUi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: carrent
 * @description:
 * @author: 高振杨
 * @create: 2022-01-12 21:05
 **/
@Service(value = "roleService")
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Override
    public TableLayUi AllRole(int page, int limit) {
        PageHelper.startPage(page,limit);
        List<Role> roles= roleDao.selectAllRole();
        PageInfo<Role> pageInfo = new PageInfo<Role>(roles);
        //pageInfo.getTotal:获取的为表中总条数
        //pageInfo.getList():获取的为当前页的记录数据
        return new TableLayUi(0,"",pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public CommonResult addOrUpdate(Role role) {
        Integer i;
        if (role.getRoleid() != null) {
            i = roleDao.updateByPrimaryKeySelective(role);
        } else {
            i = roleDao.insertSelective(role);
        }
        CommonResult result;
        if (i > 0) {
            result = CommonResult.success((Object) null, "操作成功");
        } else {
            result = CommonResult.fail("操作失败");
        }
        return result;
    }

    @Override
    public TableLayUi findRoleByPage(int page, int limit) {
        PageHelper.startPage(page,limit);
        List<Role> roles = roleDao.findAll();
        PageInfo<Role> pageInfo = new PageInfo<Role>(roles);
        return new TableLayUi(0,"", (int) pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public CommonResult delById(Integer roleid) {
        Integer i = this.roleDao.deleteByPrimaryKey(roleid);
        CommonResult result;
        if (i > 0) {
            result = CommonResult.success((Object)null, "删除成功");
        } else {
            result = CommonResult.fail("删除失败");
        }

        return result;
    }
}
